package com.hdc.lw.controller;

import com.alibaba.fastjson.JSON;
import com.hdc.lw.config.RedisPrefix;
import com.hdc.lw.po.Response;
import com.hdc.lw.po.User;
import com.hdc.lw.service.UserServiceImpl;
import com.hdc.lw.utils.SMSUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * 测试
     *
     * @return
     */
    @RequestMapping("test")
    public String test() {
        return JSON.toJSONString("请求成功！");
    }

    /**
     * 登录验证
     *
     * @param request
     * @return
     */
    @PostMapping("login")
    public Object gerUser(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(null, username, password, null, null, null, null, null, null);
        return userService.getUserByName(user);
    }

    /**
     * 注册
     *
     * @param request
     * @return
     */
    @RequestMapping("register")
    public Object register(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(null, username, password, null, null, new Date(), null, null, null);
        return userService.registerUser(user);
    }

    /**
     * 发手机验证码
     */
    @PostMapping("code/{phone}")
    public Object sendCode(@PathVariable("phone") String phone) {
        return userService.sendCode(phone);
        // 判断手机号在60s内是否发送过短信
//        if (stringRedisTemplate.hasKey(phone+ RedisPrefix.THROTTLE)) {
//            throw new RuntimeException("您已经在60s内发送过短信，请稍后重试~");
//        }
//        try {
//            String code = RandomStringUtils.randomNumeric(6);
//            System.out.println("code = " + code);
//            // 调用封装好的发送验证码的工具类进行短信发送
//             SMSUtils.sendMsg(phone, code);
//
//            // 将验证码保存到redis中
//            stringRedisTemplate.opsForValue().set(phone + RedisPrefix.THROTTLE, code, 60, TimeUnit.SECONDS);  //发送频率限制
//            stringRedisTemplate.opsForValue().set(phone + RedisPrefix.OUT, code, 30, TimeUnit.MINUTES);    // 验证码存活时间
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("短信发送异常");
//        }
//        return new Response(Response.COMMON_SUCCESS,"验证码发送成功！",null);
    }

    /**
     * 手机登录
     * @param request
     * @return
     */
    @GetMapping("login")
    public Object loginByPhone(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        String code = request.getParameter("code");
        return userService.findUserByPhone(phone, code);
    }

    /**
     * 获取用户列表
     * @param page 目标获取页数
     * @param rows 每页获取几条数据
     * @param name 模糊匹配用户名
     */
    @RequestMapping("users")
    public Object queryAllUsers(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "3") Integer rows, String name) {
        return userService.findAllUsers(page, rows, name);
    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @RequestMapping("find/{id}")
    public Object getUserById(@PathVariable("id")  Integer id){
        return userService.getUserById(id);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public Object deleteUserById(@PathVariable("id")  Integer id){
        return userService.deleteUserById(id);
    }

    /**
     * 根据id修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("update")
    public Object updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    // 近7天注册情况
    @RequestMapping("getRegisterWeek")
    public Object getRegisterWeek() {
        return userService.registerWeek();
    }

    // 近7天登录情况
    @RequestMapping("getLoggedWeek")
    public Object getLoggedWeek() {
        return userService.loggerWeek();
    }

    // 统计手机绑定
    @RequestMapping("getPhone")
    public Object getPhone() {
        return userService.getPhone();
    }
}
